package com.dynamic.manager.controller.admin.system;


import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dynamic.model.model.po.SystemCode;
import com.dynamic.model.service.SystemCodeServiceI;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dynamic.model.common.action.BaseController;
import com.dynamic.model.common.service.BaseService;
import com.dynamic.model.common.vo.Page;
import com.dynamic.model.common.vo.Result;

@Controller
@RequestMapping(value="admin/systemcode")
public class SystemCodeController extends BaseController<SystemCode>{
	private static final Logger LOGGER = Logger.getLogger(SystemCodeController.class);

	@Override
	@Resource
	public void setBaseService(BaseService<SystemCode> baseService) {
		this.baseService = baseService;
	}
	
	private SystemCodeServiceI getService(){
		return (SystemCodeServiceI) this.baseService;
	}
	
	@RequestMapping(value = "tocodelist")
	@ResponseBody
	public ModelAndView tocodelist() {
		ModelAndView mav = new ModelAndView();
		Map<String,String> nav=new LinkedHashMap<String,String>();
		nav.put("后台","#");
		nav.put("系统管理","#");
		nav.put("系统常量","/admin/systemcode/tocodelist");
		mav.addObject("navmap", nav);
		mav.setViewName("jsp/systemcodelist");
		return mav;
	}
	
	@RequestMapping(value = "getcodelist")
	@ResponseBody
	public void getcodelist(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String order = request.getParameter("order");
		String name = request.getParameter("departmentname");
		if(null!=offset&&null!=limit){
			Page<SystemCode> page=new Page<SystemCode>();
			page.setOffset(Integer.parseInt(offset));
			page.setLimit(Integer.parseInt(limit));
			page.setOrder(order);
			List<SystemCode> list=this.getService().getPageDataByName(page, name);
			page.setRows(list);
			this.renderJson(page, response);
		}else{
			Result result=new Result();
			result=Result.fail("参数错误");
			this.renderJson(result, response);
		}
	}
	
	@Override
	protected Result beforeDoSave(HttpServletRequest request,HttpServletResponse response, SystemCode entity) throws Exception {
		if(null!=entity.getCodeKey()&&entity.getCodeKey().length()>0){
			if(null!=entity.getCodeValue()&&entity.getCodeValue().length()>0){
				SystemCode code=this.getService().getCodeByKey(entity.getCodeKey());
				if(null==code){
					return Result.ok();
				}else{
					return Result.fail("键名必须唯一");
				}
			}else{
				return Result.fail("键值不能为空");
			}
		}else{
			return Result.fail("键名不能为空");
		}
		
	}
}
